package com.infocollect.project.wx.controller;

import com.infocollect.common.utils.StringUtils;
import com.infocollect.framework.web.domain.AjaxResult;
import com.infocollect.project.grant.domain.*;
import com.infocollect.project.grant.service.*;
import com.infocollect.project.wx.domain.ExaminerSceneDto;
import com.infocollect.project.wx.domain.ExaminerSceneRpComplexDto;
import com.infocollect.project.wx.domain.ExaminerSceneRpDto;
import com.infocollect.project.wx.domain.UserRegisterDto;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 签到相关操作
 */
@AllArgsConstructor
@RestController
@RequestMapping("/wx")
public class ExaminerSceneController {
    private static final Logger log = LoggerFactory.getLogger(ExaminerSceneController.class);
    @Autowired
    private IGrExaminerSceneService examinerSceneService;
    @Autowired
    private IGrGrantService grantService;
    @Autowired
    private IGrSceneService sceneService;
    @Autowired
    private IGrExaminerService examinerService;
    @Autowired
    private IGrExaminerGrantService examinerGrantService;
    @Autowired
    private IGrGranttypeService granttypeService;
    @Autowired
    private IGrExaminerService grExaminerService;

    /**
     * 根据项目项目、场次和证件号码获取签到相关信息
     * @param examinerSceneDto
     * @return
     */
    @ResponseBody
    @PostMapping("getExaminerSceneInfoForIdcard")
    public AjaxResult getExaminerSceneInfoForIdcard(@RequestBody ExaminerSceneDto examinerSceneDto){
        Long grantId = examinerSceneDto.getGrantId();
        Long sceneId = examinerSceneDto.getSceneId();
        String idCard = examinerSceneDto.getIdCard();
        if(grantId==null || sceneId==null || StringUtils.isEmpty(idCard)){
            return AjaxResult.error("错误的签到信息！");
        }
        GrGrant grant = grantService.selectGrGrantById(grantId);
        if(grant==null){
            return AjaxResult.error("错误的签到信息！");
        }
        GrScene scene = sceneService.selectGrSceneById(sceneId);
        if(scene==null){
            return AjaxResult.error("错误的签到信息！");
        }
        GrExaminerSceneNew examinerScene = new GrExaminerSceneNew();
        examinerScene.setGrantId(grantId);
        examinerScene.setSceneId(sceneId);
        examinerScene.setIdCard(idCard);
        examinerScene.setStatus(-1);
        List<GrExaminerSceneNew> examinerSceneNewList = examinerSceneService.selectGrExaminerSceneList(examinerScene);
        if(examinerSceneNewList.size()>0){
            GrExaminerSceneNew examinerSceneNew = examinerSceneNewList.get(0);
            examinerSceneNew.setGrantName(grant.getGrantName());
            examinerSceneNew.setSceneName(scene.getSceneName());
            return AjaxResult.success(examinerSceneNew);
        }
        return AjaxResult.error("未找到该人员的签到信息");
    }
    /**
     * 根据项目项目、场次和openId获取签到相关信息
     * @param examinerSceneDto
     * @return
     */
    @ResponseBody
    @PostMapping("getExaminerSceneInfoForOpenid")
    public AjaxResult getExaminerSceneInfoForOpenid(@RequestBody ExaminerSceneDto examinerSceneDto){
        Long grantId = examinerSceneDto.getGrantId();
        Long sceneId = examinerSceneDto.getSceneId();
        String openId = examinerSceneDto.getOpenId();
        GrExaminer examiner = new GrExaminer();
        examiner.setWechatId(openId);
        List<GrExaminer> examinerList= examinerService.selectGrExaminerList(examiner);
        if(examinerList.size()>0){
            examiner = examinerList.get(0);
            examinerSceneDto.setIdCard(examiner.getIdcard());
            return getExaminerSceneInfoForIdcard(examinerSceneDto);
        }
        return AjaxResult.error("未找到该微信的人员报名信息");
    }
    @ResponseBody
    @PostMapping("examinerSign")
    public AjaxResult examinerSign(@RequestBody ExaminerSceneDto examinerSceneDto){
        AjaxResult result = getExaminerSceneInfoForIdcard(examinerSceneDto);
        if(200!=(Integer)result.get("code")){
            return result;
        }
        GrExaminerSceneNew examinerSceneNew = (GrExaminerSceneNew)result.get("data");
        Long id = examinerSceneNew.getId();
        GrExaminerScene examinerScene = examinerSceneService.selectGrExaminerSceneById(id);
        if(examinerScene.getSceneStatus() == 1)
        {
            examinerScene.setRemark("重复签到");
            return AjaxResult.success(examinerScene);
        }
        examinerScene.setSceneStatus(1);
        examinerScene.setTime(new Date());
        examinerSceneService.updateGrExaminerScene(examinerScene);
        return AjaxResult.success(examinerScene);
    }
    /**
     * @author dongpj
     * @date 2022/7/7 21:05
     * @param examinerSceneDto
     * @return com.infocollect.framework.web.domain.AjaxResult
     * @describe 根据openId获取用户信息，要判断用户是不是在这个项目里面。
     */
    @ResponseBody
    @PostMapping("getExaminerInfoForOpenId")
    public AjaxResult getExaminerInfoForOpenId(@RequestBody ExaminerSceneDto examinerSceneDto){
        String openId = examinerSceneDto.getOpenId();
        String idCard = "";
        Long examinerId = 0l;
        if(StringUtils.isNotEmpty(openId)){
            GrExaminer grExaminer = new GrExaminer();
            grExaminer.setWechatId(openId);
            List<GrExaminer> examinerList =  examinerService.selectGrExaminerList(grExaminer);
            if(examinerList.size()>0){
                grExaminer = examinerList.get(0);
                idCard = grExaminer.getIdcard();
                examinerId = grExaminer.getId();
            }
        }
        if(StringUtils.isEmpty(idCard)){
            return AjaxResult.error("请先进行报名登记");
        }
        Long sceneId = examinerSceneDto.getSceneId();
        GrScene scene = sceneService.selectGrSceneById(sceneId);
        if(scene==null){
            return AjaxResult.error("错误的场次信息，请联系管理员");
        }
        Long grantId = scene.getGrantId();
        GrGrant grant = grantService.selectGrGrantById(grantId);
        GrExaminerGrantDto examinerGrantDto = new GrExaminerGrantDto();
        examinerGrantDto.setExaminerId(examinerId);
        examinerGrantDto.setGrantId(grantId);
        List<GrExaminerGrantDto> examinerGrantDtoList = examinerGrantService.selectGrExaminerGrantDtoList(examinerGrantDto);
        if(examinerGrantDtoList.size()==0){
            return AjaxResult.error("未找到报名信息,请先进行报名登记");
        }
        examinerGrantDto = examinerGrantDtoList.get(0);
        return AjaxResult.success(examinerGrantDto);
    }
    /**
     * @author dongpj
     * @date 2022/7/7 21:05
     * @param examinerSceneDto
     * @return com.infocollect.framework.web.domain.AjaxResult
     * @describe 根据替换页面传入姓名，获取要被替换的人员列表
     */
    @ResponseBody
    @PostMapping("getRpExaminerList")
    public AjaxResult getRpExaminerList(@RequestBody ExaminerSceneDto examinerSceneDto){
        String examinerName = examinerSceneDto.getExaminerName();
        GrExaminerSceneNew examinerScene = new GrExaminerSceneNew();
        examinerScene.setExaminerIdText(examinerName);
        examinerScene.setSceneId(examinerSceneDto.getSceneId());
        List<GrExaminerSceneNew> examinerSceneList = examinerSceneService.selectGrExaminerSceneList(examinerScene);

        if(examinerSceneList.size()>0) {
            Long grantId= examinerSceneList.get(0).getGrantId();
            GrGrant grant = grantService.selectGrGrantById(grantId);
            GrGranttype granttype = granttypeService.selectGrGranttypeById(grant.getGranttypeId());
            examinerSceneList.get(0).setGrantType(granttype.getInputType());
        }
        return AjaxResult.success(examinerSceneList);
    }
    /**
     * @author dongpj
     * @date 2022/7/7 21:05
     * @param examinerSceneRpDto
     * @return com.infocollect.framework.web.domain.AjaxResult
     * @describe 人员替换操作
     */
    @ResponseBody
    @PostMapping("examinerReplace")
    public AjaxResult examinerReplace(@RequestBody ExaminerSceneRpDto examinerSceneRpDto){
        Long sceneId = examinerSceneRpDto.getSceneId();
        Long examinerId = examinerSceneRpDto.getExaminerId();
        Long rpExaminerId = examinerSceneRpDto.getRpExaminerId();
        Long examinerSceneId = examinerSceneRpDto.getExaminerSceneId();
        GrExaminerSceneNew examinerSceneNew = new GrExaminerSceneNew();
        examinerSceneNew.setExaminerId(examinerId);
        examinerSceneNew.setSceneId(sceneId);
        examinerSceneNew.setStatus(-1);
        List<GrExaminerSceneNew> examinerSceneNewList = examinerSceneService.selectGrExaminerSceneList(examinerSceneNew);
        if(examinerSceneNewList.size()>0){
            return AjaxResult.error("你已在本场考试中，不能再替换他人");
        }
        GrExaminerScene  examinerScene = examinerSceneService.selectGrExaminerSceneById(examinerSceneId);
        examinerScene.setReplaceExaminerId(rpExaminerId);
        examinerScene.setExaminerId(examinerId);
        examinerScene.setRemark("人员替换");
        examinerSceneService.updateGrExaminerScene(examinerScene);
        return AjaxResult.success();
    }

    /**
     * @author huzj
     * @date 2023/5/27 19:20
     * @param examinerSceneRpDto
     * @return com.infocollect.framework.web.domain.AjaxResult
     * @describe 人员替换复杂操作
     */
    @ResponseBody
    @PostMapping("examinerReplaceComplex")
    public AjaxResult examinerReplaceComplex(@RequestBody ExaminerSceneRpComplexDto examinerSceneRpDto){
        //先判断当前人员信息是否存在，不存在做插入，存在做更新
        GrExaminer grExaminer = null;
        if(examinerSceneRpDto.getExaminerId()!=null)
        {
            grExaminer = grExaminerService.selectGrExaminerById(examinerSceneRpDto.getExaminerId());
        }
        else
        {
            grExaminer = new GrExaminer();
            grExaminer.setIdcard(examinerSceneRpDto.getIdCard());
            List<GrExaminer> grExaminerList = grExaminerService.selectGrExaminerList(grExaminer);
            if(grExaminerList.size()>0)
            {
                grExaminer = grExaminerList.get(0);
            }

        }
        grExaminer.setName(examinerSceneRpDto.getName());
        grExaminer.setPhone(examinerSceneRpDto.getPhone());
        if(!"".equals(examinerSceneRpDto.getStaffImg())) {
            grExaminer.setPhotoUrl(examinerSceneRpDto.getStaffImg());
        }
        grExaminer.setPhone(examinerSceneRpDto.getPhone());
        grExaminer.setSex(examinerSceneRpDto.getSex());
        grExaminer.setPost(examinerSceneRpDto.getPost());
        grExaminer.setTechnicalTitle(examinerSceneRpDto.getTechnicalTitle());
        if(examinerSceneRpDto.getExaminerId()!=null)
        {
            grExaminerService.updateGrExaminer(grExaminer);
        }
        else
        {
            grExaminerService.insertGrExaminer(grExaminer);
            GrExaminer grExaminerQuery = new GrExaminer();
            grExaminerQuery.setIdcard(examinerSceneRpDto.getIdCard());
            List<GrExaminer> examinerList =  grExaminerService.selectGrExaminerList(grExaminerQuery);
            if(examinerList.size()>0){
                grExaminer = examinerList.get(0);
            }
        }
        //判断一下这个人员有没有报名
        Long sceneId = examinerSceneRpDto.getSceneId();
        Long examinerId = grExaminer.getId();
        GrExaminerGrant curExaminerGrant = null;
        GrExaminerGrant examinerGrant = new GrExaminerGrant();
        examinerGrant.setExaminerId(grExaminer.getId());
        examinerGrant.setGrantId(examinerSceneRpDto.getGrantId());
        List<GrExaminerGrant> examinerGrantList = examinerGrantService.selectGrExaminerGrantList(examinerGrant);
        //如果没有报过名
        if(examinerGrantList.size()==0)
        {
            examinerGrant.setExaminerId(examinerSceneRpDto.getRpExaminerId());
            examinerGrant.setGrantId(examinerSceneRpDto.getGrantId());
            examinerGrantList = examinerGrantService.selectGrExaminerGrantList(examinerGrant);
            if(examinerGrantList.size()>0) {
                curExaminerGrant = examinerGrantList.get(0);
                curExaminerGrant.setId(null);
                curExaminerGrant.setExaminerId(grExaminer.getId());
                curExaminerGrant.setBlankCark(examinerSceneRpDto.getBankCode());
                curExaminerGrant.setDepositName(examinerSceneRpDto.getOpenBank());
                curExaminerGrant.setBlankPhone(examinerSceneRpDto.getBankMobile());
                examinerGrantService.insertGrExaminerGrant(curExaminerGrant);

            }
        }
        else
        {
            curExaminerGrant = examinerGrantList.get(0);
            curExaminerGrant.setExaminerId(grExaminer.getId());
            curExaminerGrant.setBlankCark(examinerSceneRpDto.getBankCode());
            curExaminerGrant.setDepositName(examinerSceneRpDto.getOpenBank());
            curExaminerGrant.setBlankPhone(examinerSceneRpDto.getBankMobile());
            examinerGrantService.updateGrExaminerGrant(curExaminerGrant);
        }


        Long rpExaminerId = examinerSceneRpDto.getRpExaminerId();
        Long examinerSceneId = examinerSceneRpDto.getExaminerSceneId();
        GrExaminerSceneNew examinerSceneNew = new GrExaminerSceneNew();
        examinerSceneNew.setExaminerId(examinerId);
        examinerSceneNew.setSceneId(sceneId);
        examinerSceneNew.setStatus(-1);
        List<GrExaminerSceneNew> examinerSceneNewList = examinerSceneService.selectGrExaminerSceneList(examinerSceneNew);
        if(examinerSceneNewList.size()>0){
            return AjaxResult.error("你已在本场考试中，不能再替换他人");
        }
        GrExaminerScene  examinerScene = examinerSceneService.selectGrExaminerSceneById(examinerSceneId);
        examinerScene.setReplaceExaminerId(rpExaminerId);
        examinerScene.setExaminerId(examinerId);
        examinerScene.setRemark("人员替换");
        examinerSceneService.updateGrExaminerScene(examinerScene);
        return AjaxResult.success();
    }
}
